floatTofrac = {
    # 0.5: "1/2",
    # 0.33: "1/3",
    # 0.25: "1/4",
    # 0.2: "1/5",
    0.167: "1/6",
    0.143: "1/7",
    # 0.125: "1/8",
    0.111: "1/9",
    # 0.1: "1/10",
    0.091: "1/11",
    0.083: "1/12",
    0.077: "1/13",
    0.071: "1/14",
    0.067: "1/15",
    0.063: "1/16",
    0.059: "1/17",
    0.056: "1/18",
    0.053: "1/19",
    0.05: "1/20",
}

fracTofloat = {
    # "1/2": 0.5,
    # "1/3": 0.333,
    # "1/4": 0.25,
    # "1/5": 0.2,
    "1/6": 0.167,
    "1/7": 0.143,
    # "1/8": 0.125,
    "1/9": 0.111,
    # "1/10": 0.1,
    "1/11": 0.091,
    "1/12": 0.083,
    "1/13": 0.077,
    "1/14": 0.071,
    "1/15": 0.067,
    "1/16": 0.063,
    "1/17": 0.059,
    "1/18": 0.056,
    "1/19": 0.053}


def main():
    while True:
        # 随机选择正向或反向记忆
        if random.choice([True, False]):
            # 正向记忆：给出百分数，要求输入对应的分数
            # 从fraTofloat字典中随机选择一个分数
            fraction = random.choice(list(fracTofloat.keys()))
            correct_float = fracTofloat[fraction]
            print(f"请输入对应的分数形式（形如1/x）：{correct_float * 100:.1f}%")

            user_input = input("你的答案：").strip()
            if user_input == fraction:
                print("正确！答案为：", fraction)
            else:
                # 用红色字体输出错误答案
                print("\033[31m错误，正确答案是：\033[0m", fraction)
        else:
            # 反向记忆：给出分数，要求输入对应的百分数
            fraction = random.choice(list(fracTofloat.keys()))
            correct_percentage = fracTofloat[fraction] * 100  # 四舍五入保留一位小数
            correct_percentage = round(correct_percentage, 1)
            print(f"请输入对应的百分数形式（形如x.x%）：{fraction}")
            user_input = input("你的答案：")
            if user_input.endswith("%"): user_input = str(user_input)[:-1]
            user_input = float(user_input)
            if user_input == correct_percentage:
                print("正确！答案为：", str(correct_percentage) + "%")
            else:
                print("\033[31m错误，正确答案是：\033[0m", str(correct_percentage) + "%")


if __name__ == "__main__":
    import random

    main()
